----------------------------------------------------------
----           ____                 _ _               ----
----          / ___|  ___ _ __ ___ | | |              ----
----          \___ \ / __| '__/ _ \| | |              ----
----           ___) | (__| | | (_) | | |              ----
----          |____/ \___|_|  \___/|_|_|              ----
----                                                  ----
----      Converts Spawn egg to Summon scroll         ----
----                                                  ----
----     All Graphics and Code Creative Commons 0     ----
----                 Sirrob Zeroone                   ----
----            Version: 20181012                     ---- 
----------------------------------------------------------
                       
----------------------------------------------------------
----                  "o1" = Cartoon                  ----
---Inspiration from: www.how-to-draw-funny-cartoons.com---
----              "o2" = Simple Stencils              ----
----     Inspiration from: All over the Internet      ----
----              Any img name starting c1_           ----
----                will override o1/o2               ----
----------------------------------------------------------
usr_iop = minetest.settings:get_bool("o1_enbled_o2_disabled")-- get user option from UI settings
if usr_iop == true then
	img_option = "o1"
elseif usr_iop == false then
	img_option = "o2"
else
	img_option = "o1"                                      -- even with a default this can resolve to nil so error protection
end		

usr_pre = minetest.settings:get("mod_scroll_pre")         -- get user tooltip prefix from UI setting, default is "Summon"
if usr_pre == nil then
	desc_prefix = "Summon"                                -- even with a default this can be nil so error protection
else
desc_prefix = minetest.settings:get("mod_scroll_pre")     -- Tooltip prefix
end

----------------------------------------------------------
----            Scroll Graphic Elements               ----
----           Created by: evandromyller              ----
----     www.openclipart.org/detail/17299/papyrus     ----
----           Modded by: Sirrob Zeroone              ----
----------------------------------------------------------

scroll_bg = "aa_scroll_blank.png"
scroll_overlay = "aa_scroll_blank_overlay.png"            -- Currently a blank image as preferred it, left in incase anyone wishes to Customise
scroll_top = "aa_scroll_blank_top.png"
fp_scrolls = minetest.get_modpath("mobs_scroll")          -- path for mobs scroll
scrolls = {}                                              -- table store for mobs_ spawn egg item

----------------------------------------------------------
----       Populate loaded mod mobs item list         ----
---- by looking for items specific to mobs spawn_egg  ----
---- also workarounds for NSSM, Goblines, Mineclone2  ----
----------------------------------------------------------

if mobs then
	for skey,value in pairs(minetest.registered_items) do                
		if minetest.registered_items[skey]
		["groups"]["spawn_egg"] ~= nil then                 -- Only add named items which are in the group spawn_egg                		
			scrolls[skey] = value                           -- Build new table of relevant item names and values                                                                                                  
		elseif string.find(tostring(minetest.registered_items[skey]
			   ["image"]),"_egg") or                        -- NSSM egg workaround, note to workout-should use pattern match "nssm" .. .* .. "_egg"
			   string.find(tostring(minetest.registered_items[skey]
			   ["description"]),"goblin egg") or            -- Goblins egg workaround     
			   string.find(tostring(minetest.registered_items[skey]
			   ["image"]),"mobs_mc_spawn_icon_")            -- Mineclone2 egg workaround			   
                  then               		
					scrolls[skey] = value
					scrolls[skey]["description"] = string.gsub(scrolls[skey]["description"]," Egg","") -- NSSM remove egg from end of description
					scrolls[skey]["description"] = string.gsub(scrolls[skey]["description"]," egg","") -- Goblin remove egg from end of description
		else
		end	
	end
else 
	--scrolls_error = "nil"                                 -- no mob entity items would be registered ie no mob spawn eggs
end

--[[ old code
if mobs then
	for skey,value in pairs(minetest.registered_items) do                
		if minetest.registered_items[skey]
		["groups"]["spawn_egg"] ~= nil then                 -- Only add named items which are in the group spawn_egg                		
			scrolls[skey] = value                           -- Build new table of relevant item names and values                                                              
			--i = i+1                                     
		else
		end	
	end
else 
	--scrolls_error = "nil"                                 -- no mob entity items would be registered ie no mob spawn eggs
end
]]--

----------------------------------------------------------
----    Replace the eggs with scrolls and selected    ----
----             graphic option/custom                ----
----------------------------------------------------------

if scrolls then
	for k,va in pairs(scrolls) do
	local img_e = "c1_"..scrolls[k]["description"] .. ".png"                           -- Create inital image string assuming a c1 (custom) image eg c1_White Sheep(Tamed).png  
	img_ex = string.gsub(img_e, "[%s()]", "")                                          -- Image strings can't contain spaces or brackets so remove these eg c1_WhiteSheepTamed.png 
	local img_ex_co, err = io.open(fp_scrolls .."/textures/".. img_ex, "rb");          -- Try to open the constructed Image string to confirm Image exists 
		if err then                                                                    -- If theres an error then c1 (custom) image dosen't exist for that spawn egg item
			local img_e = img_option .."_".. scrolls[k]["description"] .. ".png"       -- Create image string using option selected - o1/o2 eg o1_White Sheep(Tamed).png
			img_ex = string.gsub(img_e, "[%s()]", "")                                  -- Image strings can't contain spaces or brackets so remove these eg o1_WhiteSheepTamed.png
			local img_ex_co, err2 = io.open(fp_scrolls .."/textures/".. img_ex, "rb"); -- Try to open the constructed Image string to confirm Image exists 
				if err2 then                                                           -- If theres an error than o1/o2 dosent exist for this spawn egg item
					img_ex = img_option.."_aa_noimage".. ".png"                        -- Resort to no image avaliable image and create working img string eg o1_aa_noimage.png
				else
					img_ex_co:close()                                                  -- Outcome here is if there is no err2 created and img string will equal o1_WhiteSheepTamed.png 
				end	                                                                   -- Also close the image file that was opened.
		else
			img_ex_co:close()                                                          -- Outcome here is if there is no err created and img string will equal c1_WhiteSheepTamed.png
		end                                                                            -- Also close the image file that was opened.
	minetest.override_item(k,{                                                         -- Standard override item using the key in scrolls (scrolls[k]) eg mobs_animal:sheep_white
	description = desc_prefix .. " " .. scrolls[k]["description"],                     -- Adjust the description of spawn item so it includes the prefix eg Summon White Sheep
	inventory_image = scroll_bg .. "^" ..                                              -- Build the Scroll Inventory image using the 3 scroll elements and our created image string
	img_ex .. "^" ..                                                                   -- eg "aa_scroll_blank.png"^"o1_WhiteSheepTamed.png"^"aa_scroll_blank_overlay.png"^"aa_scroll_blank_top.png"
	scroll_overlay .. "^" .. 
	scroll_top,
	})		
	end
end
                                                
	

